package com.example.platosreports;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
//import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class Monthly extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		//Get the date from intent
		Intent intent = getIntent();
		String date = intent.getStringExtra(DailySearch.THIS_DATE);
		
		setContentView(R.layout.activity_monthly);
		
		try {
			monthlyInput(date);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		setupActionBar();
	}

	private void monthlyInput(String date) throws IOException
	{
		String filename = "M" + date + ".txt";
		File inFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath(), filename);	
		
		String netSales = null;
		String margin = null;
		String taxable = null;
		String taxCollected = null;
		String buys = null;
		String avgTran= null;
		String pctAccessories = null;
		String pctMBasic = null;
		String pctMfw = null;
		String pctMss = null;
		String pctWBasic = null;
		String pctWfw = null;
		String pctWss = null;
		
		
		if(inFile.exists())
		{
		
			FileInputStream tryThis = new FileInputStream(inFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(tryThis));
		
		
			//int count = 1;  //counter to get to needed line
			String data2 = "Empty";  //hold input string
			//First two lines are always garbage
			data2 = myReader.readLine();
			data2 = myReader.readLine();
			int flag = 1;  //Flag to get out of loop when value is found
			String [] inputArr;
			
			
//Gets Net Sales
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("Total"))
				{
					if(inputArr[1].equals("Net"))
					{
						if(inputArr[2].equals("Sales"))
						{
							margin = inputArr[4];
							netSales = inputArr[5];
							flag = -999;
						}
						else
						{
							flag = 1;
						}
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}

//Find taxable sales
			flag = 1;
			while(flag != -999)
			{	
				data2 = myReader.readLine();
				inputArr = data2.split(" +");
				
				if(inputArr[0].equals("Net"))
				{
					if(inputArr[1].equals("Taxable"))
					{
						if(inputArr[2].equals("Sales"))
						{	
							taxable = inputArr[3];
							flag = -999;
						}
						else
						{
							flag = 1;
						}
					}
					else 
					{
						flag = 1;
					}
				}		
				else
				{
					flag = 1;
				}
			}

//Find tax collected
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
				
			if(inputArr[0].equals("Tax"))
			{
				if(inputArr[1].equals("Collected"))
				{
					taxCollected = inputArr[4];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
		
//Find Total Buys
		
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
				
			if(inputArr[0].equals("USED"))
			{
				if(inputArr[1].equals("TOTAL"))
				{
					buys = inputArr[3];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
		
//Find Average Transaction Total
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
				
			if(inputArr[0].equals("Average"))
			{
				if(inputArr[1].equals("Trans."))
				{
					avgTran = inputArr[2];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
		
//Accessories Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
				
			if(inputArr[0].equals("10"))
			{
				if(inputArr[1].equals("ACCESSORIES"))
				{
					pctAccessories = inputArr[10];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
		
//Men's Basic Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
						
			if(inputArr[0].equals("20"))
			{
				if(inputArr[1].equals("MENS"))
				{
					pctMBasic = inputArr[11];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
				
//Men's Fall/Winter Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
						
			if(inputArr[0].equals("30"))
			{
				if(inputArr[1].equals("MENS"))
				{
					pctMfw = inputArr[11];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}

				
//Men's Spring/Summer Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
				
			if(inputArr[0].equals("40"))
			{
				if(inputArr[1].equals("MENS"))
				{
					pctMss = inputArr[11];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
				
//Women's Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
						
			if(inputArr[0].equals("50"))
			{
				if(inputArr[1].equals("WOMENS"))
				{
					pctWBasic = inputArr[11];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
				
//Women's Fall/Winter Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
						
			if(inputArr[0].equals("60"))
			{
				if(inputArr[1].equals("WOMENS"))
				{
					pctWfw = inputArr[11];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}
				
//Women's Spring/Sumer Percent of Sales
		flag = 1;
		while(flag != -999)
		{	
			data2 = myReader.readLine();
			inputArr = data2.split(" +");
						
			if(inputArr[0].equals("70"))
			{
				if(inputArr[1].equals("WOMENS"))
				{
					pctWss = inputArr[11];
					flag = -999;
				}
				else 
				{
					flag = 1;
				}
			}		
			else
			{
				flag = 1;
			}
		}				
						
			//Close Reader
			myReader.close();
	}
		else
		{
			TextView testData2 = (TextView) findViewById(R.id.textView);
			testData2.setText("File Not Found");
		}
		
		//Print out all variables
		netSales = ("Net Sales \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020$" + netSales);
		TextView disnetSales = (TextView) findViewById(R.id.textView1);
		disnetSales.setText(netSales);
		margin = ("Margin \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 " + margin + "%");
		TextView dismargin = (TextView) findViewById(R.id.textView2);
		dismargin.setText(margin);
		taxable = ("Taxable Sales \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020$" + taxable);
		TextView distaxable = (TextView) findViewById(R.id.textView3);
		distaxable.setText(taxable);
		taxCollected = ("Tax Collected \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 $" + taxCollected);
		TextView distaxCollected = (TextView) findViewById(R.id.textView4);
		distaxCollected.setText(taxCollected);
		avgTran = ("Average Sales Per Trans. \u0020$" + avgTran);
		TextView disavgTran = (TextView) findViewById(R.id.textView5);
		disavgTran.setText(avgTran);
		pctAccessories = ("Acc. % of Sales \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020\u0020" + pctAccessories + "%");
		TextView disAcc = (TextView) findViewById(R.id.textView7);
		disAcc.setText(pctAccessories);
		pctMBasic = ("Men's Basic % of Sales \u0020 \u0020 \u0020\u0020 \u0020" + pctMBasic + "%");
		TextView dismb = (TextView) findViewById(R.id.textView8);
		dismb.setText(pctMBasic);
		pctMfw = ("Men's F/W % of Sales \u0020 \u0020 \u0020 \u0020 \u0020 \u0020" + pctMfw + "%");
		TextView dismfw = (TextView) findViewById(R.id.textView9);
		dismfw.setText(pctMfw);
		pctMss = ("Men's S/S % of Sales \u0020 \u0020 \u0020 \u0020 \u0020\u0020 \u0020" + pctMss + "%");
		TextView dismss = (TextView) findViewById(R.id.textView10);
		dismss.setText(pctMss);
		pctWBasic = ("Women's Basic % of Sales \u0020 \u0020" + pctWBasic + "%");
		TextView diswb = (TextView) findViewById(R.id.textView11);
		diswb.setText(pctWBasic);
		pctWfw = ("Women's F/W % of Sales \u0020 \u0020 \u0020 " + pctWfw + "%");
		TextView diswfw = (TextView) findViewById(R.id.textView12);
		diswfw.setText(pctWfw);
		pctWss = ("Women's S/S % of Sales \u0020 \u0020 \u0020 \u0020" + pctWss + "%");
		TextView diswss = (TextView) findViewById(R.id.textView13);
		diswss.setText(pctWss);
		buys = ("Buys \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 \u0020 $" + buys);
		TextView disbuys = (TextView) findViewById(R.id.textView6);
		disbuys.setText(buys);
	}
	
	
	
	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.monthly, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			Intent intent1 = new Intent(this, PlatosReports.class);
			intent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent1);
			
		}
		return super.onOptionsItemSelected(item);
	}

}
